package com.example.demoapplication;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
//import android.support.v7.widget.RecyclerView;

import androidx.recyclerview.widget.RecyclerView;

import java.util.ArrayList;

public class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder> {//继承RecyclerView.Adapter
    private ArrayList<String> mData;

    public MyAdapter(ArrayList<String> data) {
        this.mData = data;//初始化数据
    }


    @Override
    //创建一个我们自定义的ViewHolder
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        // 实例化要展示的view布局
        View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_rv, parent, false);
        // 实例化viewholder
        ViewHolder viewHolder = new ViewHolder(v);
        return viewHolder;
    }


    @Override
    //绑定ViewHolder
    public void onBindViewHolder(ViewHolder holder, int position) {
        // ViewHolder中的view与数据进行绑定
        holder.mTv.setText(mData.get(position));
        holder.mtv.setText(mData.get(position) + " hell owe");
    }

    @Override
    //Item数量
    public int getItemCount() {
        return mData == null ? 0 : mData.size();
    }

    public static class ViewHolder extends RecyclerView.ViewHolder {

        TextView mTv,mtv;

        public ViewHolder(View itemView) {//根据实际需求，持有不同的对象。
            super(itemView);
            mTv = itemView.findViewById(R.id.tv_item);
            mtv = itemView.findViewById(R.id.mtv);

        }
    }
}